function varargout = mouse(varargin)
% MOUSE M-file for mouse.fig
%      MOUSE, by itself, creates a new MOUSE or raises the existing
%      singleton*.
%
%      H = MOUSE returns the handle to a new MOUSE or the handle to
%      the existing singleton*.
%
%      MOUSE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOUSE.M with the given input arguments.
%
%      MOUSE('Property','Value',...) creates a new MOUSE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mouse_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mouse_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
 
% Edit the above text to modify the response to help mouse
 
% Last Modified by GUIDE v2.5 18-Mar-2011 21:54:43
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mouse_OpeningFcn, ...
                   'gui_OutputFcn',  @mouse_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
 
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
 
 
% --- Executes just before mouse is made visible.
function mouse_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mouse (see VARARGIN)
 
% Choose default command line output for mouse
imaqreset;
Allenable_off(hObject,eventdata,handles);
handles.output = hObject;
guidata(hObject, handles);
handles.M(1:8)=0;
guidata(hObject, handles);
import java.awt.Robot; % ??java.awt.Robot?
import java.awt.event.*;
handles.mouse = Robot; % ??java
% Update handles structure
guidata(hObject, handles);
handles.k=230;
guidata(hObject, handles);
handles.imageformat='RGB';
guidata(hObject, handles);
handles.screenSize=get(0,'screensize');
guidata(hObject,handles);
% UIWAIT makes mouse wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
%Video Initialization
info=imaqhwinfo;
[l,n]=size(info.InstalledAdaptors);
if n==0
    ms=msgbox('There have no any Adaptors.','error','error');
    return
end
set(handles.popupmenu1,'string',info.InstalledAdaptors,'value',1);
[handles.obj,handles.imageformat]=Video_select(hObject,eventdata,handles,'initialization');
guidata(hObject,handles);
pause(1.5);
ms=questdlg('Are you want to setup Parameters?','Setup','Yes','No','Yes');
if strcmp(ms,'Yes')
    pushbutton3_Callback(hObject, eventdata, handles);
end
Allenable_on(hObject,eventdata,handles);
 
% --- Outputs from this function are returned to the command line.
function varargout = mouse_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Get default command line output from handles structure
varargout{1} = handles.output;
 
 
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Allenable_off(hObject,eventdata,handles);
ms=msgbox('Please put off red glass!');
flag=0;
while flag==0
    a=getsnapshot(handles.obj);
    flushdata(handles.obj);
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k-5);
    if flag==3
        Allenable_on(hObject,eventdata,handles);
        return
    end
    if ~ishandle(ms)
        ms=questdlg('Are you sure exit this position?','','Yes','No','Yes');
        if strcmp(ms,'Yes')
            Allenable_on(hObject,eventdata,handles);
            return
        else
            ms=msgbox('Please put off red glass!');
        end
    end
end
if ishandle(ms)
    delete(ms);
end
pause(1)
!d:\mouse\position.exe
pause(0.8);
a1=getsnapshot(handles.obj);
if strcmp(handles.imageformat,'YUY')
    a1=ycbcr2rgb(a1);
elseif strcmp(handles.imageformat,'HSV')
    a1=hsv2rgb(a1);
elseif ~strcmp(handles.imageformat,'RGB')
    ms=msgbox('The Video image cannot conversion!','error','error');
    return
end
a2=rgb2gray(a1);
a2=medfilt2(a2);
a3=im2bw(a2);
% se=strel('rectangle',[2 2]);
% a3=imdilate(a3,se);
% figure,imshow(a3)
[h,w]=size(a3);
h1=fix(h/10);
w1=fix(w/10);
if h>=240
    q=[
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
        ];
    ginny=160;
elseif h<240
    q=[
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
        ];
    ginny=100;
end
[xg(1),yg(1)]=Image_model(1,fix(h/2),1,fix(w/2),a3,q,ginny);
[xg(2),yg(2)]=Image_model(1,fix(h/2),fix(w/2),w,a3,q,ginny);
[xg(3),yg(3)]=Image_model(fix(h/2),h,1,fix(w/2),a3,q,ginny);
[xg(4),yg(4)]=Image_model(fix(h/2),h,fix(w/2),w,a3,q,ginny);
for i=1:4
    if xg(i)==0|yg(i)==0
        ms=questdlg('Would you want again?','','Yes','No','Yes');
        if strcmp(ms,'Yes')
            pushbutton1_Callback(hObject,eventdata,handles);
        elseif ~strcmp(ms,'Yes')
            Allenable_on(hObject,eventdata,handles);
            return
        end
    end
end
M=Video_M(hObject,handles,xg,yg);
handles.M=M;
guidata(hObject,handles);
ms=msgbox('Please put on red glass!');
flag=1;
while flag>0
    a=getsnapshot(handles.obj);
    flushdata(handles.obj);
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k);%Have a problem?Why ans=1?   
    if ~ishandle(ms)
        ms=questdlg('Are you sure exit this time position?','','Yes','No','Yes');
        if strcmp(ms,'Yes')
            Allenable_on(hObject,eventdata,handles);
            return
        elseif ~strcmp(ms,'Yes')
            ms=msgbox('Please put on red glass!');
        end
    end
end
if ishandle(ms)
    delete(ms);
end
pause(3);
main(hObject,eventdata,handles);
 
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Allenable_off(hObject,eventdata,handles);
ms=msgbox('Please put on red glass!');
flag=1;
while flag>0
    a=getsnapshot(handles.obj);
    flushdata(handles.obj);
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k);
    if flag==3
        return
    end    
    if ~ishandle(ms)
        ms=questdlg('Are you sure exit this position?','','Yes','No','Yes');
        if strcmp(ms,'Yes')
            Allenable_on(hObject,eventdata,handles);
            return
        elseif ~strcmp(ms,'Yes')
            ms=msgbox('Please put on red glass!');
        end
    end
end
if ishandle(ms)
    delete(ms);
end
X(1)=handles.screenSize(4)/10;X(2)=handles.screenSize(4)/10;X(3)=9*handles.screenSize(4)/10;X(4)=9*handles.screenSize(4)/10;
Y(1)=handles.screenSize(3)/10;Y(2)=9*handles.screenSize(3)/10;Y(3)=handles.screenSize(3)/10;Y(4)=9*handles.screenSize(3)/10;
for i=1:4
    pause(1.5)
    handles.mouse.mouseMove(Y(i),X(i));
    ms=msgbox('Please point the spot');
    while 1
        a=getsnapshot(handles.obj);
        flushdata(handles.obj);
        [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k);
        if flag==3
            return
        end        
        if ~ishandle(ms)
            ms=questdlg('Are you sure exit this position?','','Yes','No','Yes');
            if strcmp(ms,'Yes')
                Allenable_on(hObject,eventdata,handles);
                return
            elseif ~strcmp(ms,'Yes')
                ms=msgbox('Please point the spot!');
            end
        end         
        if flag==1
            axis_x(i)=x;
            axis_y(i)=y;           
            delete(ms)
            break
        end
    end
end
M=Video_M(hObject,handles,axis_x,axis_y);
handles.M=M;
guidata(hObject,handles);
main(hObject, eventdata, handles);
 
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Allenable_off(hObject,eventdata,handles);
ms=msgbox('Please put on red glass!');
while ishandle(ms)
    pause(0.2)
end
ms=questdlg('Have you put red glass?','','Yes','No','Yes');
if strcmp(ms,'No')
    Allenable_on(hObject,eventdata,handles);
    return
end
pause(0.2)
a=getsnapshot(handles.obj);
flushdata(handles.obj);
flag=1;
k1=100;
while flag>0
    k1=k1+1;
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,k1);
    if flag==3
        Allenable_on(hObject,eventdata,handles);
        return
    end    
end
k1=k1+10;
ms=msgbox('Please touch computer screen!');
flag=0;
while flag==0
    if ~ishandle(ms)
        ms=questdlg('Are you sure exit this Setup?','','Yes','No','Yes');
        if strcmp(ms,'Yes')
            Allenable_on(hObject,eventdata,handles);
            return
        elseif ~strcmp(ms,'Yes')
            ms=msgbox('Please touch computer screen!');
        end
    end
    a=getsnapshot(handles.obj);
    flushdata(handles.obj);
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,k1);   
end
if ishandle(ms)
    delete(ms)
end
k2=k1;
while flag>0
    k2=k2+1;
    [x,y,flag,I]=Bright_spots(handles.imageformat,a,k2);
    if flag==3
        return
    end    
end
handles.k=round((k1+k2)/2);
guidata(hObject,handles);
ms=msgbox('Setup is OK,you can use it.');
pause(2)
delete(ms)
Allenable_on(hObject,eventdata,handles);
 
% --- Executes on button press in pushbutton3.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;
 
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
!help.txt
 
% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
 
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: place code in OpeningFcn to populate axes1
axis off
 
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[obj,imageformat]=Video_select(hObject,eventdata,handles,'first');
if obj~=0
    handles.obj=obj;
    guidata(hObject,handles);
    handles.imageformat=imageformat;
    guidata(hObject,handles);
end
 
 
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
 
 
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
 
 
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[obj,imageformat]=Video_select(hObject,eventdata,handles,'second');
if obj~=0
    handles.obj=obj;
    guidata(hObject,handles);
    handles.imageformat=imageformat;
    guidata(hObject,handles);
end
% Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
 
 
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
 
% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[obj,imageformat]=Video_select(hObject,eventdata,handles,'third');
if obj~=0
    handles.obj=obj;
    guidata(hObject,handles);
    handles.imageformat=imageformat;
    guidata(hObject,handles);
end
% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3
 
 
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
 
 
function main(hObject, eventdata, handles)
import java.awt.Robot; % ??java.awt.Robot?
import java.awt.event.*;
mouse = Robot; % ??java
%Detection position
% ms=msgbox('Please point "OK"');
% while ishandle(ms)
%         a=getsnapshot(handles.obj);
%         flushdata(handles.obj);
%         [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k);
%         if flag==3
%             return
%         end       
%         if flag>0
%             x2=(handles.M(1)*x+handles.M(2)*y+handles.M(3))/(handles.M(7)*x+handles.M(8)*y+1);
%             y2=(handles.M(4)*x+handles.M(5)*y+handles.M(6))/(handles.M(7)*x+handles.M(8)*y+1);
%             x2=max(1,x2);
%             x2=min(x2,handles.screenSize(4));
%             y2=max(1,y2);
%             y2=min(y2,handles.screenSize(3));            
%             mouse.mouseMove(y2,x2);
%             mouse.mousePress(InputEvent.BUTTON1_MASK);
%             mouse.mouseRelease(InputEvent.BUTTON1_MASK);
%         end
% end
% ms=questdlg('If you make sure position,press "Enter" key.','','Yes','No','Yes');
% if strcmp(ms,'No')
%     Allenable_on(hObject,eventdata,handles);
%     return
% end
 
x_h=0;y_h=0;t=0;s=0;mouse_left=0;timer1=0;
main_figure=msgbox('Are you want to exit?');
while ishandle(main_figure)
        a=getsnapshot(handles.obj);
        flushdata(handles.obj);
        [x,y,flag,I]=Bright_spots(handles.imageformat,a,handles.k);
    if flag==3
        return
    end
        if timer1==1
            t=t+1;
            if t==5
                t=0;
                timer1=0;
            end
        end
        if flag>0
            if flag==1
                x2=(handles.M(1)*x+handles.M(2)*y+handles.M(3))/(handles.M(7)*x+handles.M(8)*y+1);
                y2=(handles.M(4)*x+handles.M(5)*y+handles.M(6))/(handles.M(7)*x+handles.M(8)*y+1);
                x2=max(1,x2);
                x2=min(x2,handles.screenSize(4));
                y2=max(1,y2);
                y2=min(y2,handles.screenSize(3));                 
                mouse.mouseMove(y2,x2);
                if mouse_left==0
                    s=sqrt((x-x_h)^2+(y-y_h)^2);
                    if t>2&s<5
                        mouse.mousePress(InputEvent.BUTTON1_MASK);
                        mouse.mouseRelease(InputEvent.BUTTON1_MASK);
                        pause(0.1)
                        mouse.mousePress(InputEvent.BUTTON1_MASK);
                        mouse.mouseRelease(InputEvent.BUTTON1_MASK);
                        timer1=0;
                        t=0;
                    else
                        timer1=1;
                        t=0;
                        mouse_left=1;
                        mouse.mousePress(InputEvent.BUTTON1_MASK);
                        x_h=x;
                        y_h=y;
                    end
                end
            else
                mouse.mousePress(InputEvent.BUTTON3_MASK);
                mouse.mouseRelease(InputEvent.BUTTON3_MASK);
            end
        else
            if mouse_left==1
                mouse.mouseRelease(InputEvent.BUTTON1_MASK);
                mouse_left=0;
            end
        end
end
Allenable_on(hObject,eventdata,handles);
 
function Allenable_off(hObject,eventdata,handles)
set(handles.pushbutton1,'enable','off');
set(handles.pushbutton2,'enable','off');
set(handles.pushbutton3,'enable','off');
set(handles.pushbutton4,'enable','off');
set(handles.pushbutton5,'enable','off');
set(handles.popupmenu1,'enable','off');
set(handles.popupmenu2,'enable','off');
set(handles.popupmenu3,'enable','off');
 
function Allenable_on(hObject,eventdata,handles)
set(handles.pushbutton1,'enable','on');
set(handles.pushbutton2,'enable','on');
set(handles.pushbutton3,'enable','on');
set(handles.pushbutton4,'enable','on');
set(handles.pushbutton5,'enable','on');
set(handles.popupmenu1,'enable','on');
set(handles.popupmenu2,'enable','on');
set(handles.popupmenu3,'enable','on');
 
function [obj,flag,imageformat]=Video_obj(hObject,eventdata,handles,str)
flag=0;
obj=0;
imageformat='NO';
if strcmp(str,'initialization')
    str=get(handles.popupmenu1,'string');
    [n,l]=size(str);
    for i=1:n
        info=imaqhwinfo(char(str(i)));
        ids=info.DeviceIDs;
        [l,n1]=size(ids);
        if n1>0
            id=cell2mat(ids(1));
            flag=1;
            obj=videoinput(char(str(i)),id);
            str1=str(i);
            str(i)=str(1);
            str(1)=str1;
            set(handles.popupmenu1,'string',str,'value',1);
            [obj,flag,imageformat]=Video_obj(hObject,eventdata,handles,'first');
            return
        end
    end
    ms=msgbox('There have no any camera.','error','error');
elseif strcmp(str,'first')
        str=get(handles.popupmenu1,'string');
        No=get(handles.popupmenu1,'value');
        info=imaqhwinfo(char(str(No)));
        ids=info.DeviceIDs;
        [l,n1]=size(ids);
        if n1==0
            set(handles.popupmenu2,'string','None','value',1);
            set(handles.popupmenu3,'string','None','value',1);
        elseif n1~=0
            flag=1;
            for i=1:n1
                id=cell2mat(ids(i));
                dev_info=imaqhwinfo(char(str(No)),id);
                dev_info.DeviceName={dev_info.DeviceName};%char->cell
                DeviceName(i)=dev_info.DeviceName;
            end   
            obj=videoinput(char(str(No)),cell2mat(ids(1)));
            dev_info=imaqhwinfo(char(str(No)),cell2mat(ids(1)));
            set(handles.popupmenu2,'string',DeviceName,'value',1);
            set(handles.popupmenu3,'string',dev_info.SupportedFormats,'value',1);
            Format=char(dev_info.DefaultFormat);
            imageformat=Video_format(Format);
        end
elseif strcmp(str,'second')
        str=get(handles.popupmenu1,'string');
        No=get(handles.popupmenu1,'value'); 
        info=imaqhwinfo(char(str(No)));
        No1=get(handles.popupmenu2,'value');
        str2=get(handles.popupmenu2,'string');
        if strcmp(str2,'None');
            return
        end
        flag=1;
        obj=videoinput(char(str(No)),No1);
        dev_info=imaqhwinfo(char(str(No)),No1);
        set(handles.popupmenu3,'string',dev_info.SupportedFormats,'value',1);
        Format=char(dev_info.DefaultFormat);
        imageformat=Video_format(Format);
elseif strcmp(str,'third')
        str=get(handles.popupmenu1,'string');
        No=get(handles.popupmenu1,'value'); 
        info=imaqhwinfo(char(str(No)));
        No1=get(handles.popupmenu2,'value');
        str3=get(handles.popupmenu3,'string');
        if strcmp(str3,'None');
            return
        end
        flag=1; 
        str1=get(handles.popupmenu3,'string');
        No2=get(handles.popupmenu3,'value');
        dev_info=imaqhwinfo(char(str(No)),No1);
        Format=char(dev_info.DefaultFormat);
        imageformat=Video_format(Format);        
        obj=videoinput(char(str(No)),No1,char(dev_info.SupportedFormats(No2)));
end
 
function [obj,imageformat]=Video_select(hObject,eventdata,handles,str)
obj=0;
imageformat='NO';
[obj,flag,imageformat]=Video_obj(hObject, eventdata,handles,str);
if flag==1
    vidRes = get(obj, 'VideoResolution'); 
    nBands = get(obj, 'NumberOfBands'); 
    handles.hImage=image(zeros(vidRes(2),vidRes(1),nBands));
    preview(obj,handles.hImage);%play video
end
 
function M=Video_M(hObject,handles,axis_x,axis_y)
M=0;
X(1)=handles.screenSize(4)/10;X(2)=handles.screenSize(4)/10;X(3)=9*handles.screenSize(4)/10;X(4)=9*handles.screenSize(4)/10;
Y(1)=handles.screenSize(3)/10;Y(2)=9*handles.screenSize(3)/10;Y(3)=handles.screenSize(3)/10;Y(4)=9*handles.screenSize(3)/10;
S=[
    axis_x(1) axis_y(1) 1 0 0 0 -axis_x(1)*X(1) -axis_y(1)*X(1)
    0 0 0 axis_x(1) axis_y(1) 1 -axis_x(1)*Y(1) -axis_y(1)*Y(1)
    axis_x(2) axis_y(2) 1 0 0 0 -axis_x(2)*X(2) -axis_y(2)*X(2)
    0 0 0 axis_x(2) axis_y(2) 1 -axis_x(2)*Y(2) -axis_y(2)*Y(2)
    axis_x(3) axis_y(3) 1 0 0 0 -axis_x(3)*X(3) -axis_y(3)*X(3)
    0 0 0 axis_x(3) axis_y(3) 1 -axis_x(3)*Y(3) -axis_y(3)*Y(3)
    axis_x(4) axis_y(4) 1 0 0 0 -axis_x(4)*X(4) -axis_y(4)*X(4)
    0 0 0 axis_x(4) axis_y(4) 1 -axis_x(4)*Y(4) -axis_y(4)*Y(4)
    ];
M=S\[X(1) Y(1) X(2) Y(2) X(3) Y(3) X(4) Y(4)]';
 
function imageformat=Video_format(Format)
imageformat='NO';
if strfind(Format,'YUY')==1
     imageformat='YUY';
elseif strfind(Format,'HSV')==1
     imageformat='HSV';
elseif strfind(Format,'RGB')==1
     imageformat='RGB';
else
     imageformat='NO';
end
 
function [x,y,flag,I]=Bright_spots(imageformat,A,k)
flag=0;I=0;
x=0;y=0;
x1=0;y1=0;
if strcmp(imageformat,'YUY')
    A=ycbcr2rgb(A);
elseif strcmp(imageformat,'HSV')
    A=hsv2rgb(A);
elseif strcmp(imageformat,'RGB')
else
    flag=3;
    ms=msgbox('The Video image cannot conversion!','error','error');
    return    
end
I=rgb2gray(A);
I=medfilt2(I);
I=I-k;
I=10*I;
I=im2bw(I);
[x,y]=find(I,1);
[x1,y1]=find(I,1,'last');
if x~=0&y~=0
    if sqrt(((x1-x)^2+(y1-y)^2))<50
        flag=1;
    elseif sqrt(((x1-x)^2+(y1-y)^2))>=50
        flag=2;
    end
end

function [x,y]=Image_model(x1,x2,y1,y2,I,q,ginny)
[gin,w]=size(q);
gin=gin/2;
[h,w]=size(I);
h1=fix(h/10);
w1=fix(w/10);
x=0;y=0;
n=0;
x1=x1+h1;
x2=x2-h1;
y1=y1+w1;
y2=y2-w1;
for i=x1:x2
    for j=y1:y2
        s1=I(i-gin:i+gin-1,j-gin:j+gin-1).*q;
        Q1=sum(sum(s1));
        if Q1>=ginny
            s2=~I(i-gin:i+gin-1,j-gin:j+gin-1).*~q;
            Q2=sum(sum(s2));
            if Q2>=ginny
                n=n+1;
                xi(n)=i;
                yi(n)=j;
            end
        end
    end
end
if n==0
    return
end
x=sum(xi)/n;
y=sum(yi)/n;
